package org.serverct.finalfight;

import com.sun.istack.internal.NotNull;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.serverct.finalfight.game.GameManager;
import org.serverct.finalfight.game.gui.SpectatorTeleporter;
import org.serverct.finalfight.game.hadesgame.EventHandler;
import org.serverct.finalfight.game.hadesgame.EventType;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.basic.GameType;
import org.serverct.finalfight.stat.gui.InfoGui;
import org.serverct.finalfight.util.CustomSettings;

//TODO RECOUNSTRUCT
public class Commands implements CommandExecutor {
    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
        try {
            this.goCommand(sender, cmd, label, args);
            return true;
        } catch (Exception exception) {
            if (exception instanceof IndexOutOfBoundsException) {
                sender.sendMessage(CustomSettings.getLanguageWithPrefix("wrongparams"));
                //TODO 帮助-Language
                sender.sendMessage(CustomSettings.getPrefix() + "§e插件指令帮助: ");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight fastjoin §7- §e快速匹配");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight join <ID> §7- §e加入指定房间");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight quit §7- §e退出房间");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight info §7- §e查看战绩");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight add <ID> §7- §e创建房间");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight load <ID> §7- §e载入指定房间");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight unload <ID> §7- §e卸载指定房间");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight reload <ID> §7- §e重载指定房间");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight spectator <ID> §7- §e进入一局的观战模式");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight start <ID> §7- §e立即开始指定房间的游戏（需要人数符合要求）");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight forceEnd <ID> §7- §e强制结束指定房间的游戏");
                sender.sendMessage(CustomSettings.getPrefix() + "§a/finalfight forceEvent <ID> [事件名] §7- §e立即触发指定房间的阴间事件");
            } else if (exception instanceof NumberFormatException) {
                sender.sendMessage(CustomSettings.getLanguageWithPrefix("notinteger"));
            } else if (exception instanceof ClassCastException) {
                sender.sendMessage(CustomSettings.getLanguageWithPrefix("notplayer"));
            } else {
                exception.printStackTrace();
                sender.sendMessage(CustomSettings.getLanguageWithPrefix("unexpectederror"));
            }
            return true;
        }
    }

    public void goCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!label.equalsIgnoreCase("finalfight")) {
            return;
        }
        Player player = (Player) sender;
        if (args[0].equalsIgnoreCase("info")) {
            player.openInventory(InfoGui.generateInventory(player));
            return;
        }
        if (args[0].equalsIgnoreCase("fastjoin")) {
            GameManager.fastJoin(player);
            return;
        }

        if (args[0].equalsIgnoreCase("add") && player.isOp()) {
            GameManager.addArena(args[1], GameType.valueOf(args[2]));
            player.sendMessage("suc");
            return;
        }
        if (args[0].equalsIgnoreCase("load") && player.isOp()) {
            GameManager.loadArena(args[1]);
            player.sendMessage("sucload");
            return;
        }
        if (args[0].equalsIgnoreCase("quit")) {
            BasicGame game = GameManager.getGame(player);
            if (game == null) {
                player.sendMessage(CustomSettings.getLanguageWithPrefix("notin"));
                return;
            }
            game.quit(player, true);
            return;
        }
        BasicGame game = GameManager.arenas.get(args[1]);
        if (game == null) {
            player.sendMessage(CustomSettings.getLanguageWithPrefix("arenanotexist"));
            return;
        }
        if (args[0].equalsIgnoreCase("join")) {
            game.join(player,false);
            return;
        }
        if (args[0].equalsIgnoreCase("spectator")) {
            game.join(player,true);
            return;
        }
        if (!player.isOp()) {
            player.sendMessage(CustomSettings.getLanguageWithPrefix("hasnopermission"));
            return;
        }
        if (args[0].equalsIgnoreCase("start")) {
            player.sendMessage(CustomSettings.getLanguageWithPrefix(game.tryToStart()));
            return;
        }
        if (args[0].equalsIgnoreCase("unload")) {
            GameManager.unloadArena(args[1]);
            player.sendMessage("sucunload");
            return;
        }
        if (args[0].equalsIgnoreCase("reload")) {
            GameManager.reloadArena(args[1]);
            player.sendMessage("sucreload");
            return;
        }
        if (args[0].equalsIgnoreCase("forceEnd")) {
            GameManager.forceStop(args[1]);
            player.sendMessage("sucstop");
            return;
        }
        if (args[0].equalsIgnoreCase("forceEvent")) {
            if (args.length == 2) {
                EventHandler.randomEvent(game);
            } else {
                EventType type = EventType.valueOf(args[2]);
                EventHandler.callEvent(game, type);
            }
            player.sendMessage(CustomSettings.getLanguageWithPrefix("eventexecuted"));
            return;
        }
        throw new IndexOutOfBoundsException();
    }
}
